#ifndef _DSWONVIF_SESSION_H
#define _DSWONVIF_SESSION_H

#include "base.h"
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavformat/avio.h>
#include "dswonvifrecoder.h"

class CDswonvif_session
{
public:
	CDswonvif_session(LPST_ENV_T pEnv, LPST_EXTERNAL_CAM_T pExtCam, int nExChId);
	virtual ~CDswonvif_session();
public:
	void SetDiskStatus(ENM_HD_STATUS nStatus);
	void ForceSave();
	LPST_EXTERNAL_CAM_DETAIL_T GetExtCamDetail() {return &m_nExtCam;};
	void GetFrameFromMem(LPST_VIDEO_FRAME_T pFrame);

	void PTZControl(int nCode, int nValue);
private:
	static void* cbRecvStreamThreadFunc(void* pParam){((CDswonvif_session*)pParam)->RecvStreamThreadFunc();return NULL;};
	void RecvStreamThreadFunc();
	
	bool GetMediaCapabilities(int* pMac, char* pUUID, char* pDeviceURL);
	bool GetProfiles(struct soap *soap, char* pUUID, char* pMediaURL);
	bool GetStreamURL(struct soap *soap, char* pMediaURL, char* profile_token, char* pStreamURL);
	bool ProcessStreamURL(int nStreamIndex, char* pStreamURL);

	static int cbInterruptMainStream(void *ctx){return ((CDswonvif_session*)ctx)->InterruptMainStream();};
	void PutFrameToMem(int nStreamIndex, AVPacket* pFrame);

	static void* cbRecvSubStreamThreadFunc(void* pParam){((CDswonvif_session*)pParam)->RecvSubStreamThreadFunc();return NULL;};
	void RecvSubStreamThreadFunc();
	static int cbInterruptSubStream(void *ctx){return ((CDswonvif_session*)ctx)->InterruptSubStream();};
	int InterruptMainStream();
	int InterruptSubStream();

	void StartRecoder();
	void StopRecoder();
	
	static void* cbAnsyTimeThreadFunc(void* pParam){((CDswonvif_session*)pParam)->AnsyTimeThreadFunc();return NULL;};
	void AnsyTimeThreadFunc();

	bool GetCamTime(int* pMac, char* pUUID, char* pDeviceURL, time_t &nTime, bool &bDaylightSavings);
	void SetCamTime(int* pMac, char* pUUID, char* pDeviceURL, time_t nTime, bool bDaylightSavings);

	static void cbCheckDiskTask(int fd, short events, void* ctx){((CDswonvif_session*)ctx)->CheckDiskTask(fd, events);};
	void CheckDiskTask(int fd, short events);
private:
	LPST_ENV_T m_pEnv;
	bool m_bStopRecvMainStream;
	pthread_t m_nRecvStreamThreadID;
	ST_EXTERNAL_CAM_DETAIL_T m_nExtCam;
	ST_VIDEO_MEM_T m_video_mem[2];

	bool m_bStopRecvSubStream;
	pthread_t m_nRecvSubStreamThreadID;
	CDswonvifrecoder* m_pRecoder;
	ENM_HD_STATUS m_nHdStatus;
	pthread_mutex_t m_nRecoderLock;

	bool m_bStopAnsyTime;
	pthread_t m_nAnsyTimeThreadID;

	time_t m_nMainStreamLastTime, m_nSubStreamLastTime;
	int m_nExChId;
	bool m_bNeedAuth;

	char m_sPtzService[256];
	bool m_bHasPTZ;

	timer_param_t m_nCheckDiskTask;
};

#endif

